package com.ruoyi.project.mydo.mydojob.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.mydo.mydojob.domain.MydoJob;
import com.ruoyi.project.mydo.mydojob.mapper.MydoJobFinishMapper;
import com.ruoyi.project.mydo.mydojob.mapper.MydoJobMapper;
import com.ruoyi.project.mydo.mydojob.service.IMydoJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作业表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class MydoJobServiceImpl implements IMydoJobService {
    @Autowired
    private MydoJobMapper mydoJobMapper;
    @Autowired
    private MydoJobFinishMapper mydoJobFinishMapper;

    /**
     * 查询作业表
     *
     * @param jobId 作业表主键
     * @return 作业表
     */
    @Override
    public MydoJob selectMydoJobByJobId(String jobId) {
        return mydoJobMapper.selectMydoJobByJobId(jobId);
    }

    /**
     * 查询作业表列表
     *
     * @param mydoJob 作业表
     * @return 作业表
     */
    @Override
    public List<MydoJob> selectMydoJobList(MydoJob mydoJob) {

        if (mydoJob.getStatus() == null || "4".compareTo(mydoJob.getStatus()) > 0) {
            return mydoJobMapper.selectMydoJobList(mydoJob);

        } else {
            return mydoJobFinishMapper.selectMydoJobFinishList(mydoJob);
        }

    }

    /**
     * 查询盘点作业表列表
     *
     * @param mydoJob 作业表
     * @return 作业表
     */
    @Override
    public List<MydoJob> selectpdMydoJobList(MydoJob mydoJob) {

        if (mydoJob.getStatus() == null || "4".compareTo(mydoJob.getStatus()) > 0) {
            return mydoJobMapper.selectpdMydoJobList(mydoJob);

        } else {
            return mydoJobFinishMapper.selectpdMydoJobFinishList(mydoJob);
        }

    }

    /**
     * 新增作业表
     *
     * @param mydoJob 作业表
     * @return 结果
     */
    @Override
    public int insertMydoJob(MydoJob mydoJob) {
        mydoJob.setCreateTime(DateUtils.getNowDate());
        return mydoJobMapper.insertMydoJob(mydoJob);
    }

    /**
     * 修改作业表
     *
     * @param mydoJob 作业表
     * @return 结果
     */
    @Override
    public int updateMydoJob(MydoJob mydoJob) {
        return mydoJobMapper.updateMydoJob(mydoJob);
    }

    /**
     * 批量删除作业表
     *
     * @param jobIds 需要删除的作业表主键
     * @return 结果
     */
    @Override
    public int deleteMydoJobByJobIds(String jobIds) {
        return mydoJobMapper.deleteMydoJobByJobIds(Convert.toStrArray(jobIds));
    }

    /**
     * 删除作业表信息
     *
     * @param jobId 作业表主键
     * @return 结果
     */
    @Override
    public int deleteMydoJobByJobId(String jobId) {
        return mydoJobMapper.deleteMydoJobByJobId(jobId);
    }

    @Override
    public AjaxResult handFinish(String jobId) {

        MydoJob inMydoJob = mydoJobMapper.selectMydoJobByJobId(jobId);
        if (inMydoJob == null) {
            return AjaxResult.error("作业不存在，请核对！");
        }
        if (!"3".equals(inMydoJob.getStatus())) {
            return AjaxResult.error("只能手动完成正执行作业，请核对！");
        }
        if (StringUtils.isBlank(inMydoJob.getCargoSpaceId())) {
            return AjaxResult.error("货位为空,不能手动完成");
        }
        String task_no = inMydoJob.getTaskNo();
        Map<String, String> map = new HashMap();

        map.put("v_task_no", task_no);
        map.put("v_result", null);
        map.put("v_resutmsg", null);
        mydoJobMapper.impJob(map);
        return AjaxResult.success(map.get("v_resutmsg"));

    }

    @Override
    public String handFinish(String trayCode, String inOrOut) {
        Map<String, Object> params = new HashMap();
        String msg = "";
        int resultCode = 0;
        params.put("trayCode", trayCode);
        if ("14".equals(inOrOut)) {
            mydoJobMapper.checkStockSure(params);
            resultCode = (int) params.get("resultCode");
            if (resultCode == 1) {
                msg = "无该条码任务，请检查！";
            } else if (resultCode == 10) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        } else if ("11".equals(inOrOut) || "10".equals(inOrOut)) {
            mydoJobMapper.outStockSure(params);
            resultCode = (int) params.get("resultCode");
            if (resultCode == 1) {
                msg = "无该条码任务，请检查！";
            } else if (resultCode == 10) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        } else if ("13".equals(inOrOut)) {
            mydoJobMapper.outStockSure(params);
            resultCode = (int) params.get("resultCode");
            if (resultCode == 1) {
                msg = "无该条码任务，请检查！";
            } else if (resultCode == 10) {
                msg = "操作成功！";
            } else {
                msg = "未知的返回值，请联系管理员！";
            }
        }
        return msg;
    }

    @Override
    public AjaxResult handInvalid(String jobId) {
        Map<String, String> map = new HashMap();
        map.put("v_job_id", jobId);
        mydoJobMapper.cancelJob(map);
        String resultCode = map.get("v_result");
        String resultMsg = map.get("v_resutmsg");
        if ("10".equals(resultCode)) {
            return AjaxResult.success(resultMsg);
        } else {
            return AjaxResult.error(resultMsg);
        }
    }

    @Override
    public Integer selectByInCount() {

        return mydoJobMapper.selectByInCount();

    }
    @Override
    public Integer selectByInCount2() {

        return mydoJobMapper.selectByInCount2();

    }

    @Override
    public Integer selectByOutCount() {

        return mydoJobMapper.selectByOutCount();
    }
    @Override
    public Integer selectByOutCount2() {

        return mydoJobMapper.selectByOutCount2();
    }

    @Override
    public List<MydoJob> selectByRoad(String mydoJob) {
        return mydoJobMapper.selectByRoad(mydoJob);
    }

    @Override
    public Integer selectJobNumByTime(String beginDate, String finishDate, String road) {
        return mydoJobMapper.selectJobNumByTime(beginDate, finishDate, road);
    }


}
